
#include "../include/N_arytree.h"







int generate_tokens(char **token_list[], char *path, char delimitor)
{
int i=0,j=0,cnt=1,cnt1=0;

char first_token[2];
first_token[0] = delimitor;
first_token[1] = '\0';

*token_list = (char**)malloc(sizeof(char*)*2);

(*token_list)[0] = (char*)malloc(sizeof(char)*100);
strcpy((*token_list)[0],first_token);

char* name;
name = (char*)malloc(sizeof(char)*100);
for(i=1;i<strlen(path);i++)
{
name[j++] = path[i];
if(path[i] == delimitor)
{
name[--j] = '\0';
*token_list = (char**)realloc(*token_list,sizeof(char*)*(cnt+1));
(*token_list)[cnt] = (char*)malloc(sizeof(char)*100);
strcpy((*token_list)[cnt],name);
for(cnt1=0;cnt1<j;cnt1++)
name[cnt1] = '\0';
j=0;
cnt++;
}
}
if(strlen(path)==1)
cnt = 0;
if(strlen(path)!=1)
{
*token_list = (char**)realloc(*token_list,sizeof(char*)*(cnt+1));
(*token_list)[cnt] = (char*)malloc(sizeof(char)*100);

strcpy((*token_list)[cnt],name);
}
//int t = 0;
cnt++;
//for(t=0;t<cnt;t++)
//printf("\nArray Contents %s:",token_list[t]);
return cnt;
}



t_node *mem_alloc()
{
	t_node * new_node;
	new_node = (t_node *)malloc(sizeof(t_node));
	
	return new_node;
}






int insert_node(t_node** root, fd* data)
{



//printf("\nFd filename in Nary insert=%s\n",data->file_name);
//printf("\nFd path in Nary insert=%s\n",data->path);


/*
t_node *cs=*root;

if((strcmp(data->file_name,"file1"))==0)
	printf("\nnaryRoot in insert 1st =%s\n",cs->data->file_name);
*/


int i,j,count=1,flag=1;
int n,c;
char **tokens=NULL;
char path[MAX_PATH_SIZE];
t_node * previous, *current;
t_node *new_node;
new_node = mem_alloc();

if(*root == NULL && (strcmp(data->file_name,"/") != 0) )
{
//allows only "/" as root node.
//printf("Invalid Root Path given.\n"); 
return 1;
}


//Insertion of the first node

if(*root == NULL)
{

//printf("Inside root null");
new_node->is_deleted = 0;
new_node->left_child = NULL;
new_node->right_siblings = NULL;
new_node->parent = NULL;
new_node->data = data;
//Root now points to newly inserted first node "/"
*root = new_node;
fdcounter++;
return 0;
}

new_node->is_deleted = 0;
new_node->left_child = NULL;
new_node->right_siblings = NULL;
new_node->parent = NULL;
new_node->data = data;
count= generate_tokens(&tokens,data->path,'/');


//Traverse the tree and check for the path
i = 0;
current = *root;
while(current != NULL)
{

if(current->is_deleted == 0 && strcmp(current->data->file_name,tokens[i]) == 0)
{




i++;
if(i == count)
break;
current = current->left_child;

}
//Goto right siblings

else if(current->right_siblings != NULL)
{
current = current->right_siblings;
}
else
{
flag =0;
break;
}
}


//Invalid positions
if(flag == 0 || current == NULL)
{
//printf("invalid path entered, given Directory/File structure do not exsist. \n");
return 1;
}

//Insertion of the new_node
if(current->left_child == NULL)
{
current->left_child=new_node;
fdcounter++;
return 0;
}

current = current->left_child;
//previous = current;


while(current->right_siblings != NULL)
{
current = current->right_siblings;

if((current->data->file_name,new_node->data->file_name)==0)
	return -1;

}
	//printf("\ncurrentnode =%s\n",new_node->data->file_name);
current->right_siblings = new_node;


fdcounter++;
return 0;
}



int delete_recursively(t_node* node)
{
if(node == NULL)
return 1;
node->is_deleted = 1;
delete_recursively(node->left_child);
while(node != NULL)
{
delete_recursively(node->right_siblings);
node = node->right_siblings;
}

fdcounter--;
return 0;
}



void printtree(t_node *root)
{
if(root!=NULL && root->is_deleted!=1)
{ 
fd* f = root->data;
printf("\nName : %s", f->file_name);
printf("\nPath : %s", f->path);
printf("\n"); 
}
if(root->left_child!=NULL)
printtree(root->left_child);
if(root->right_siblings!=NULL)
printtree(root->right_siblings);

}





int delete_node(t_node** root, char *fullpath)
{

//printf("*************Indelete node**********fullpath=%s\n",fullpath);

t_node * current;
int flag =1;
int result;

if (*root == NULL)
return 1;

int n, i,j,count = 1,c;
char **tokens;
n = strlen(fullpath);
if(n==1)
count = 1;
else
{
for(i=1;i<n;i++){
if(fullpath[i]=='/')
count++;
}
count++;
}


tokens= (char **)malloc(sizeof(char*)*count);
i = 0;
for(c=0;c<count;c++)
{
j = 0;
tokens[c] = (char *)malloc(sizeof(char) * 50);
while(fullpath[i] != '/' && fullpath[i]!='\0')
{
tokens[c][j] = fullpath[i];
i++;
j++;
}
i++;
tokens[c][j] = '\0';
}
tokens[0][0] = '/';
tokens[0][1] = '\0';
i = 0;
current = *root;


while(current != NULL)
{
if(current->is_deleted == 0 && strcmp(current->data->file_name,tokens[i]) == 0)
{
i++;
if(i == count)
break;
current = current->left_child;
}
//Goto right siblings
else if(current->right_siblings != NULL)
{
current = current->right_siblings;
}
else
{
flag =0;
break;
}
}
//Invalid positions
if(flag == 0 || current == NULL)
{
return 1;
}
//current->is_deleted = TRUE;


current->is_deleted = 1;
//current = current->left_child;

if(current->data->fileType=='D')
{
result = delete_recursively(current);
return result;
}


//printf("*************end of delete node**********fullpath=%s\n",fullpath);
return 0;
}




fd* returnnode(t_node** root, char* path)
{

//printf("\npath in return node=%s\n",path);


fd* rNode = NULL;
t_node* current=NULL;
int count=0,i=0,flag=1;

if(*root == NULL)
{
//printf("No node present in the N-array tree !!");
return rNode;
}
//printf("in return node\n");
//generate tokens in path.
char **tokens = NULL;
count = generate_tokens( &tokens,path,'/');

current = *root;
i=0;
//reach node with given path
while(current != NULL)
{
if(current->is_deleted == 0 && strcmp(current->data->file_name,tokens[i]) == 0)
{

i++;
if(i == count)
break;
current = current->left_child;
//printf("case 1\n");
}
//Goto right siblings
else if(current->right_siblings != NULL)
{
current = current->right_siblings;
//printf("case 2\n");
}
else
{
flag =0;
//printf("case 3\n");
break;
}
}
//Invalid positions

if(flag == 0 || current == NULL)
{
//printf("invalid node, check path.\n");
return rNode;

}





//current->is_deleted = TRUE;
rNode = current->data;
//printf("in return node\n");
return rNode;

}
